
local assert     = assert
local require    = require
local bit        = bit
local coroutine  = coroutine
local debug      = debug
local io         = io
local pairs      = pairs
local ipairs     = ipairs
local math       = math
local os         = os
local print      = print
local pcall      = pcall
local xpcall     = xpcall
local rawget     = rawget
local rawset     = rawset
local select     = select
local string     = string
local table      = table
local tonumber   = tonumber
local tostring	 = tostring
local error      = error
local type       = type
local unpack     = unpack
local setmetatable = setmetatable
local getmetatable = getmetatable
local ngx = ngx

local string_char  = string.char
local string_sub   = string.sub
local string_byte  = string.byte
local string_gsub  = string.gsub
local string_find  = string.find
local string_upper = string.upper

local math_fmod   = math.fmod
local math_floor  = math.floor
local math_pow    = math.pow
local math_abs    = math.abs

local table_insert = table.insert
local table_sort = table.sort

local os_date = os.date;
local table_concat = table.concat;


local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local ns_time    = require("modules.commhm.time");
local ns_network = require("modules.commhm.network");
local ns_utf8    = require("modules.commhm.utf8");

local ns_auth_comm_checker = require("modules.commhm.auth_comm_checker")

local CHtmlTable = require( "modules.html_page.CHtmlTable" )


local table_tostring = table.tostring
local table_loadstring = table.loadstring



local FOO = {
	_VERSION = '0.12.1',

	show_txt = {},

	now_ = 0,
};


FOO.header = [[<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dc_list</title>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/angular.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/cookie.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/md5.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/jquery-3.1.1.min.js"></script>
<script src="https://indevelop.mini1.cn:8088/oa/js_lib/miniajax.js"></script>
<style type="text/css">
	body,td,th { font-size: 15px; }
</style>
</head>
<body>]];


FOO.tail = [[
</body>
</html>
]];


FOO.show = function( data_org_, ctrl_ )

	-- 排序
	local data_ = {}
	for name_, ip_data_ in pairs( data_org_ ) do
		ip_data_.name = name_
		data_[ #data_ + 1 ] = ip_data_
	end

	local function sort_func_(a, b)
		return a.name < b.name
	end
	table_sort( data_, sort_func_ )

	local all_tj_ = { tps=0, b=0 }


	FOO.clean_txt()
	FOO.add_txt( FOO.header )

	FOO.now = uu.now()
	--FOO.add_txt( '<script type="text/javascript"> var g_now=' .. FOO.now .. '; </script>\n' )
	FOO.add_txt( '<h3 style="font-size: 20px;" >data_cache 运行状态列表</h3>\n' )

	FOO.add_txt( '<table border=0 cellpadding=10>\n<tr><td valign="top">\n' )   --1 all
	for seq_, ip_data_ in pairs( data_ ) do
		FOO.add_txt( '<table style="float:left;" border=2 cellpadding=2>\n' )   --2 name
		FOO.add_txt( '<tr><td style="font-size: 16px;" align="center"><b>'
				.. seq_ .. ' ' .. ip_data_.name .. '</b></td></tr>\n' )   --3 ip

		FOO.add_txt( '<tr><td>\n' )
		FOO.add_txt( '<table border=1 cellpadding=0>\n<tr>' )   --3 ip

		local ip_seq_ = 0
		for ip_, rp_list_ in pairs( ip_data_ ) do
			if  ip_ == 'name' then
				-- ignore
			else
				ip_seq_ = ip_seq_ + 1

				FOO.add_txt( '<td valign="top">' )

				FOO.add_txt( '<table border=1 cellpadding=2>\n' )   --4 cmd
				local rt_time_ = ''
				if  rp_list_.rt then
					local tt_ = uu.now() - rp_list_.rt
					if  tt_ > 60 then
						rt_time_ = '<font color="#FF0000"><b>rt=' .. tt_ .. '</b></font>'
					else
						rt_time_ = 'rt=' .. tt_
					end
				end
				FOO.add_txt( '<tr><td align="center">' .. ip_seq_ ..  '</td></tr><tr><td>' .. ip_ .. '<br>'
						.. rt_time_ .. '<br>' .. (rp_list_.ver_lua or 'no_ver_lua')
						.. '<br>' .. (rp_list_.ver_hot or 'no_ver_hot') .. '</td></tr>\n' )

				if  rp_list_.select then
					--FOO.add_txt( '<tr><td>' .. uu.to_str(rp_list_.select) .. '</td></tr>' )
					local select_str_ = ''
					for i=0, 9, 1 do
						if  rp_list_.select[ '' .. i ] then
							select_str_ = select_str_ .. i .. '=' .. math_floor(rp_list_.select[ '' .. i ]/30) .. ' '

							all_tj_.tps = all_tj_.tps + rp_list_.select[ '' .. i ]  --全部统计
						end
					end
					if  #select_str_ > 0 then
						FOO.add_txt( '<tr><td><font color="#0000FF">' .. select_str_ .. '</font></td></tr>\n' )
					end
				else
					FOO.add_txt( '<tr><td><font color="#FF0000">' .. 'no_select' .. '</font></td></tr>\n' )
				end

				if  rp_list_.list then
					local function sort_func( a, b )
						return a.c > b.c
					end
					table_sort( rp_list_.list, sort_func )

					for i=1, #rp_list_.list  do
						FOO.add_txt( '<tr><td>' )

						local b_str_ = ''
						if  rp_list_.list[i].b then
							if  rp_list_.list[i].b > 1024*1024 then
								b_str_ = math_floor( (rp_list_.list[i].b or 0) / 30 / (1024*1024) ) .. 'm'
							else
								b_str_ = math_floor( (rp_list_.list[i].b or 0) / 30 / 1024 ) .. 'k'
							end

							all_tj_.b = all_tj_.b + rp_list_.list[i].b
						end

						local str_ = rp_list_.list[i].cmd  .. ' ' .. math_floor((rp_list_.list[i].c or 0) / 30 ) .. '/' .. b_str_
						FOO.add_txt( str_ )

						if  rp_list_.list[i].b and rp_list_.list[i].c and
								(rp_list_.list[i].b / rp_list_.list[i].c > 10240) then
							FOO.add_txt( '<br><font color="#FF0000">pack_huge='
									.. math_floor( rp_list_.list[i].b / rp_list_.list[i].c ) .. '</font>' )
						end

						FOO.add_txt( '</td></tr>\n' )
					end
				end

				FOO.add_txt( '</table>\n' )   --4 cmd
				FOO.add_txt( '</td>' )
			end
		end

		FOO.add_txt( '</tr>\n</table>\n' )   --3 ip

		FOO.add_txt( '</td></tr>\n</table>\n' )   --2 name
	end

	FOO.add_txt( '</td></tr>\n</table>\n' )   --1 all

	local txt_ = '<hr><h1>全网调用量(每秒): ' .. math_floor(all_tj_.b/30) .. '(流量) / ' .. math_floor(all_tj_.tps/30)
			.. '(tps) = ' .. math.floor( all_tj_.b / all_tj_.tps ) .. '(平均包大小) </h1>'

	table.insert (FOO.show_txt, 2, txt_)

	FOO.add_txt( FOO.tail )
	ngx.say( FOO.get_txt() )

end


-----------------------------------------------------------
---清理输出
FOO.clean_txt = function()
	FOO.show_txt = {};
end

---增加输出内容
FOO.add_txt = function( txt_ )
	FOO.show_txt[ #FOO.show_txt + 1 ] = txt_
end


---获得所有输出
FOO.get_txt = function()
	return table_concat( FOO.show_txt )
end


return FOO;